/*
 *	This file is part of Transdroid <http://www.transdroid.org>
 *	
 *	Transdroid is free software: you can redistribute it and/or modify
 *	it under the terms of the GNU General Public License as published by
 *	the Free Software Foundation, either version 3 of the License, or
 *	(at your option) any later version.
 *	
 *	Transdroid is distributed in the hope that it will be useful,
 *	but WITHOUT ANY WARRANTY; without even the implied warranty of
 *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *	GNU General Public License for more details.
 *	
 *	You should have received a copy of the GNU General Public License
 *	along with Transdroid.  If not, see <http://www.gnu.org/licenses/>.
 *	
 */
package org.martyn.danbodroid.gui.adapters;

import java.util.ArrayList;
import java.util.List;

import org.martyn.danbodroid.R;
import org.martyn.danbodroid.gui.PreferenceRowDanbooru;
import org.martyn.danbodroid.gui.PreferenceRowDivider;
import org.martyn.danbodroid.gui.PreferenceRowSingle;
import org.martyn.danbooru.DanbooruConnectSettings;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

public class PreferencesAdapter extends BaseAdapter
{
	public static final String ADD_NEW_DANBOORUSERVER = "add_new_danbooruserver";
	public static final String INTERFACE_SETTINGS = "interface_settings";

	private Context context;
	private List<Object> items;

	/**
	 * Constructor voor de MainPreferenceActivity
	 * 
	 * @param context
	 *            De MainPreferenceActivity Context
	 * @param dservers
	 *            Alle Danbooru server settings
	 */
	public PreferencesAdapter(
			Context context,
			ArrayList<DanbooruConnectSettings> dservers)
	{
		this.context = context;
		this.items = new ArrayList<Object>();

		this.items.add(new PreferenceRowDivider(context,
				R.string.pref_category_danbooru_servers));
		this.items.add(new PreferenceRowSingle(context, ADD_NEW_DANBOORUSERVER,
				R.string.pref_add_danbooru));

		for (int i = 0; i < dservers.size(); i++)
		{
			this.items.add(new PreferenceRowDanbooru(context, dservers.get(i)));
		}
		this.items.add(new PreferenceRowDivider(context,
				R.string.pref_category_danbodroid));
		this.items.add(new PreferenceRowSingle(context, INTERFACE_SETTINGS,
				R.string.pref_interface_preferences));
	}

	@Override
	public int getCount()
	{
		return items.size();
	}

	@Override
	public Object getItem(int position)
	{
		return items.get(position);
	}

	@Override
	public long getItemId(int position)
	{
		return position;
	}

	@Override
	public boolean areAllItemsEnabled()
	{
		return false;
	}

	@Override
	public boolean isEnabled(int position)
	{
		// Always enabled, except when the item is a Divider
		return !(getItem(position) instanceof PreferenceRowDivider);
	}

	@Override
	/**
	 * Geeft the view terug voor de lijst item, dit kan een danbooruPreferenceRow, PreferenceDivider of een PreferenceRow zijn
	 * 
	 */
	public View getView(int position, View convertView, ViewGroup parent)
	{
		Object item = getItem(position);

		if (item instanceof PreferenceRowDanbooru)
		{
			return (PreferenceRowDanbooru) item;
		}
		else if (item instanceof PreferenceRowDivider)
		{
			// Directly return the button/divider view
			return (PreferenceRowDivider) item;
		}
		else if (item instanceof PreferenceRowSingle)
		{
			// Directly return the button/divider view
			return (PreferenceRowSingle) item;
		}

		return null;
	}
}
